package me.helllp.bootman.common.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.helllp.bootman.common.base.BaseBean;

public class BeanUtil {
	public static void transBean2Map(Map<String, Object> map, Object obj) {
        if(obj == null){
            return;
        }
        
        if(!(obj instanceof BaseBean)){
        	return;
        }
        
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), obj.getClass().getSuperclass());
            
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 得到property对应的getter方法
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);

                if(value != null){
                	if(value instanceof Class){
                		map.put(key, ((Class<?>)value).getName());
                	}else if(value instanceof List){
                		Map<String, Object> tempMap = new HashMap<String, Object>();
                		
                		List<?> list = (List<?>)value;
                		for(Object o : list){
                			transBean2Map(tempMap, o);
                		}
                		
                		map.put(key, tempMap);
                	}else{
                		map.put(key, value);	
                	}
                		
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	
	/**
	 * 将Bean转化为Map
	 * 
	 * @param obj
	 * @return
	 */
    public static Map<String, Object> transBean2Map(Object obj) {

        if(obj == null){
            return null;
        }
        
        if(!(obj instanceof BaseBean)){
        	return null;
        }
        
        Map<String, Object> map = new HashMap<String, Object>();
        
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), obj.getClass().getSuperclass());

            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);

                    if(value != null){
                    	map.put(key, value);	
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }
    
    /**
     * 将Map转化为Bean
     * 
     * @param map
     * @param obj
     */
    public static void transMap2Bean(Map<String, Object> map, Object obj) {

        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                if (map.containsKey(key)) {
                    Object value = map.get(key);
                    // 得到property对应的setter方法
                    Method setter = property.getWriteMethod();
                    setter.invoke(obj, value);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return;

    }     
}
